<?php
// +----------------------------------------------------------------------
// Csmmeet [ Csmonline ]
// Author: chensm <chenshiming0802@163.com>
// Create by chensm at 2020-03-31
// +----------------------------------------------------------------------
namespace addons\csmonline\library;

class CsmonlineCron
{
    public static function getUserLogFirstId($statdate)
    {
        $dao = new \app\admin\model\csmonline\Firstid();
        $row = $dao->where("statdate", "=", $statdate)->find();

        if ($row) {
            return $row->firstid;
        } else {
            return 99999;
        }
    }

    // http://127.0.0.1/fastadmin_plugin_csmmeet/public/addons/csmonline/Crontab/runoneday
    public static function crononedate($statdate, $isforce)
    {
        $dao = new \app\admin\model\csmonline\Userlog();

        $daocron = new \app\admin\model\csmonline\Crontab();
        $row = $daocron->where("statdate", "=", $statdate)->find();

        if ($isforce === false) {
            if ($row != null && $row->issuccess == 'Y') {
                return;
            }
        }

        // 记录运行时间
        $id = 0;
        if ($row == null) {
            $param = [
                "statdate" => $statdate,
                "issuccess" => "N",
                "starttime" => time(),
                "createtime" => time()
            ];
            $daocron->create($param);
            $id = $dao->getLastInsID();
        } else {
            $param = [
                "statdate" => $statdate,
                "issuccess" => "N",
                "starttime" => time()
            ];
            $daocron->where("id", "=", $row->id)->update($param);
            $id = $row->id;
        }

        // $firstid = CsmonlineCron::_getFirstid($dao, $statdate)
        $firstid = CsmonlineCron::getUserLogFirstId($statdate);

        // 如果一个用户登录后，需要将之前未登录的数据转为登录数据，否则所有登录过用户都算入游客
        if (true) {
            $list = $dao->alias("t")
                ->where("id", ">=", $firstid)
                ->where("createdate", "=", $statdate)
                ->group('t.browserid')
                ->having("COUNT(DISTINCT t.`isloginuser`)>1")
                ->field('browserid')
                ->select();
            // echo $dao->getLastSql();
            // var_dump($list);die();
            foreach ($list as $row) {
                $rowy = $dao->alias("t")
                    ->where("id", ">=", $firstid)
                    ->where("createdate", "=", $statdate)
                    ->where("isloginuser", "=", "Y")
                    ->where("browserid", "=", $row->browserid)
                    ->find();
                if ($rowy) {
                    $param = [
                        "isloginuser" => "Y",
                        "userid" => $rowy->userid,
                        "user_id" => $rowy->user_id,
                        "username" => $rowy->username
                    ];
                    $dao->where("id", ">=", $firstid)
                        ->where("createdate", "=", $statdate)
                        ->where("isloginuser", "=", "N")
                        ->where("browserid", "=", $row->browserid)
                        ->update($param);
                }
            }
        }

        CsmonlineCron::calcuserlog();
        CsmonlineCron::generatefauser();
        CsmonlineCron::_piestatdate($dao, $statdate, $firstid);
        CsmonlineCron::_piestathour($dao, $statdate, $firstid);
        CsmonlineCron::_pieregion($dao, $statdate, $firstid);

        CsmonlineCron::_pieenv($dao, $statdate, $firstid, "calismobile");
        CsmonlineCron::_pieenv($dao, $statdate, $firstid, "calplatform");
        CsmonlineCron::_pieenv($dao, $statdate, $firstid, "calbrowser");

        if (true) {
            $param = [
                "issuccess" => "Y",
                "endtime" => time(),
                "updatetime" => time()
            ];
            $daocron->where("id", "=", $id)->update($param);
        }
    }

    // 计算userlog的附加信息
    public static function calcuserlog()
    {
        $dao = new \app\admin\model\csmonline\Userlog();
        $list = $dao->where("iscal", "=", "N")->select();
        $csmip = Csmip::getInstance();
        foreach ($list as $val) {
            $ua = new CI_User_agent($val->useragent);
            // var_dump($val->useragent);die();
            $region = $csmip->getRegion($val->ip);
            $param = [
                "iscal" => "Y",
                "calccountry" => $region->country,
                "calcregion" => $region->region,
                "calcprovince" => $region->echartprovince,
                "calccity" => $region->city,
                "calcisp" => $region->isp,

                "calismobile" => ($ua->is_mobile == '1') ? 'Y' : 'N',
                "calplatform" => $ua->platform,
                "calbrowser" => $ua->browser,
                "calbrowserversion" => $ua->version,
                "calrobot" => $ua->robot
            ];
            $dao->where("id", "=", $val->id)->update($param);
        }
    }

    private static function _piestatdate($dao, $statdate, $firstid)
    {
        $daoline = new \app\admin\model\csmonline\Piestatdate();
        $ipy = $dao->alias("t")
            ->where("isloginuser", "=", 'Y')
            ->where("id", ">=", $firstid)
            ->where("createdate", "=", $statdate)
            ->count("distinct ip");
        $ipn = $dao->alias("t")
            ->where("isloginuser", "=", 'N')
            ->where("id", ">=", $firstid)
            ->where("createdate", "=", $statdate)
            ->count("distinct ip");
        // Unique Visitor,一天内同一访客的多次访问只记录为一个访客。
        $uvy = $dao->alias("t")
            ->where("isloginuser", "=", 'Y')
            ->where("id", ">=", $firstid)
            ->where("createdate", "=", $statdate)
            ->count("distinct userid");
        $uvn = $dao->alias("t")
            ->where("isloginuser", "=", "N")
            ->where("id", ">=", $firstid)
            ->where("createdate", "=", $statdate)
            ->count("distinct userid");
        // Page View 用户每一次对网站中的每个网页访问均被记录一次
        $pvy = $dao->alias("t")
            ->where("isloginuser", "=", 'Y')
            ->where("id", ">=", $firstid)
            ->where("createdate", "=", $statdate)
            ->count("distinct useridmd5url");
        $pvn = $dao->alias("t")
            ->where("isloginuser", "=", 'N')
            ->where("id", ">=", $firstid)
            ->where("createdate", "=", $statdate)
            ->count("distinct useridmd5url");
        // Visit View，访客访问的次数
        $vvy = $dao->alias("t")
            ->where("isloginuser", "=", 'Y')
            ->where("id", ">=", $firstid)
            ->where("createdate", "=", $statdate)
            ->count();
        $vvn = $dao->alias("t")
            ->where("isloginuser", "=", 'N')
            ->where("id", ">=", $firstid)
            ->where("createdate", "=", $statdate)
            ->count();

        // PC
        $pcusy = $dao->alias("t")
            ->where("isloginuser", "=", 'Y')
            ->where("id", ">=", $firstid)
            ->where("createdate", "=", $statdate)
            ->where("calismobile", "=", 'N')
            ->count("distinct userid");
        $pcusn = $dao->alias("t")
            ->where("isloginuser", "=", 'N')
            ->where("id", ">=", $firstid)
            ->where("createdate", "=", $statdate)
            ->where("calismobile", "=", 'N')
            ->count("distinct userid");

        // Mobile
        $mobileusy = $dao->alias("t")
            ->where("isloginuser", "=", 'Y')
            ->where("id", ">=", $firstid)
            ->where("createdate", "=", $statdate)
            ->where("calismobile", "=", 'Y')
            ->count("distinct userid");
        $mobileusn = $dao->alias("t")
            ->where("isloginuser", "=", 'N')
            ->where("id", ">=", $firstid)
            ->where("createdate", "=", $statdate)
            ->where("calismobile", "=", 'Y')
            ->count("distinct userid");

        // 计算总注册人数和新增人数
        $begintime = strtotime($statdate . " 00:00:00");
        $endtime = strtotime($statdate . " 23:59:59");
        $daofauser = new \app\admin\model\csmonline\Fauser();
        $allregisteredus = $daofauser->alias('t')
            ->where("jointime", "<=", $endtime)
            ->count();
        $newregisteredus = $daofauser->alias('t')
            ->where("jointime", "<=", $endtime)
            ->where("jointime", ">=", $begintime)
            ->count();

        $row = $daoline->where("statdate", "=", $statdate)->find();
        $dau = (($allregisteredus == 0) ? 0 : round($uvy / $allregisteredus, 3)) * 100;
        $param = [
            "statdate" => $statdate,
            "sweekday" => date("w", strtotime($statdate)),
            "ipsy" => $ipy,
            "pvsy" => $pvy,
            "uvsy" => $uvy,
            "vvsy" => $vvy,
            "pcusy" => $pcusy,
            "mobileusy" => $mobileusy,
            "ipsn" => $ipn,
            "pvsn" => $pvn,
            "uvsn" => $uvn,
            "vvsn" => $vvn,
            "pcusn" => $pcusn,
            "mobileusn" => $mobileusn,
            "allregisteredus" => $allregisteredus,
            "newregisteredus" => $newregisteredus,
            'dau' => $dau,
            "stattime" => time()
        ];
        if ($row) {
            $daoline->where("id", "=", $row->id)->update($param);
        } else {
            $daoline->create($param);
        }
    }

    // 由于在线人数有持续时间，比如用户10点访问，访问到12点，则需要统计在10点，11点和12点，所以无法通过group计算，而是要通过遍历24小时计算
    private static function _piestathour_uv($dao, $statdate, $firstid, &$pielist)
    {
        for ($i = 0; $i < 24; $i ++) {
            $ll = $dao->alias("t")
                ->where("id", ">=", $firstid)
                ->where("createdate", "=", $statdate)
                ->where("createhour", '>=', $i)
                ->where("durationlasthour", "<=", $i)
                ->field("isloginuser,count(distinct userid) cnt")
                ->group("isloginuser")
                ->select();
            // echo $dao->getLastSql();
            foreach ($ll as $row) {
                if ($row->isloginuser == 'Y') {
                    $pielist[$i]['uvy'] = $row;
                } else {
                    $pielist[$i]['uvn'] = $row;
                }
            }
        }
    }

    private static function _piestathour($dao, $statdate, $firstid)
    {
        $pielist = CsmonlineCron::_inithourpielist();

        $daoline = new \app\admin\model\csmonline\Piestathour();
        $ipy = $dao->alias("t")
            ->where("isloginuser", "=", 'Y')
            ->where("id", ">=", $firstid)
            ->where("createdate", "=", $statdate)
            ->field("createhour,count(distinct ip) cnt")
            ->group("createhour")
            ->select();
        CsmonlineCron::_adjusthourlist($pielist, "ipy", $ipy);
        $ipn = $dao->alias("t")
            ->where("isloginuser", "=", 'N')
            ->where("id", ">=", $firstid)
            ->where("createdate", "=", $statdate)
            ->field("createhour,count(distinct ip) cnt")
            ->group("createhour")
            ->select();
        CsmonlineCron::_adjusthourlist($pielist, "ipn", $ipn);
        // Unique Visitor,一天内同一访客的多次访问只记录为一个访客。
        CsmonlineCron::_piestathour_uv($dao, $statdate, $firstid, $pielist);
        // $uvy = $dao->alias("t")
        // ->where("isloginuser", "=", 'Y')
        // ->where("id", ">=", $firstid)
        // ->where("createdate","=",$statdate)
        // ->field("createhour,count(distinct userid) cnt")
        // ->group("createhour")
        // ->select();
        // CsmonlineCron::_adjusthourlist($pielist, "uvy", $uvy);
        // $uvn = $dao->alias("t")
        // ->where("isloginuser", "=", "N")
        // ->where("id", ">=", $firstid)
        // ->where("createdate","=",$statdate)
        // ->field("createhour,count(distinct userid) cnt")
        // ->group("createhour")
        // ->select();
        // CsmonlineCron::_adjusthourlist($pielist, "uvn", $uvn);
        // Page View 用户每一次对网站中的每个网页访问均被记录一次
        $pvy = $dao->alias("t")
            ->where("isloginuser", "=", 'Y')
            ->where("id", ">=", $firstid)
            ->where("createdate", "=", $statdate)
            ->field("createhour,count(distinct useridmd5url) cnt")
            ->group("createhour")
            ->select();
        CsmonlineCron::_adjusthourlist($pielist, "pvy", $pvy);
        $pvn = $dao->alias("t")
            ->where("isloginuser", "=", 'N')
            ->where("id", ">=", $firstid)
            ->where("createdate", "=", $statdate)
            ->field("createhour,count(distinct useridmd5url) cnt")
            ->group("createhour")
            ->select();
        CsmonlineCron::_adjusthourlist($pielist, "pvn", $pvn);
        // Visit View，访客访问的次数
        $vvy = $dao->alias("t")
            ->where("isloginuser", "=", 'Y')
            ->where("id", ">=", $firstid)
            ->where("createdate", "=", $statdate)
            ->field("createhour,count(*) cnt")
            ->group("createhour")
            ->select();
        CsmonlineCron::_adjusthourlist($pielist, "vvy", $vvy);
        $vvn = $dao->alias("t")
            ->where("isloginuser", "=", 'N')
            ->where("id", ">=", $firstid)
            ->where("createdate", "=", $statdate)
            ->field("createhour,count(*) cnt")
            ->group("createhour")
            ->select();
        CsmonlineCron::_adjusthourlist($pielist, "vvn", $vvn);

        // PC
        $pcusy = $dao->alias("t")
            ->where("isloginuser", "=", 'Y')
            ->where("id", ">=", $firstid)
            ->where("createdate", "=", $statdate)
            ->where("calismobile", "=", 'N')
            ->field("createhour,count(distinct userid) cnt")
            ->group("createhour")
            ->select();
        CsmonlineCron::_adjusthourlist($pielist, "pcusy", $pcusy);
        $pcusn = $dao->alias("t")
            ->where("isloginuser", "=", 'N')
            ->where("id", ">=", $firstid)
            ->where("createdate", "=", $statdate)
            ->where("calismobile", "=", 'N')
            ->field("createhour,count(distinct userid) cnt")
            ->group("createhour")
            ->select();
        CsmonlineCron::_adjusthourlist($pielist, "pcusn", $pcusn);

        // Mobile
        $mobileusy = $dao->alias("t")
            ->where("isloginuser", "=", 'Y')
            ->where("id", ">=", $firstid)
            ->where("createdate", "=", $statdate)
            ->where("calismobile", "=", 'Y')
            ->field("createhour,count(distinct userid) cnt")
            ->group("createhour")
            ->select();
        CsmonlineCron::_adjusthourlist($pielist, "mobileusy", $mobileusy);
        $mobileusn = $dao->alias("t")
            ->where("isloginuser", "=", 'N')
            ->where("id", ">=", $firstid)
            ->where("createdate", "=", $statdate)
            ->where("calismobile", "=", 'Y')
            ->field("createhour,count(distinct userid) cnt")
            ->group("createhour")
            ->select();
        CsmonlineCron::_adjusthourlist($pielist, "mobileusn", $mobileusn);

        // 计算总注册人数和新增人数
        $begintime = strtotime($statdate . " 00:00:00");
        $endtime = strtotime($statdate . " 23:59:59");
        $daofauser = new \app\admin\model\csmonline\Fauser();
        $allregisteredus = $daofauser->alias('t')
            ->where("jointime", "<=", $endtime)
            ->field("joinhour createhour,count(*) cnt")
            ->group("joinhour")
            ->select();
        CsmonlineCron::_adjusthourlist($pielist, "allregisteredus", $allregisteredus);
        $newregisteredus = $daofauser->alias('t')
            ->where("jointime", "<=", $endtime)
            ->where("jointime", ">=", $begintime)
            ->field("joinhour createhour,count(*) cnt")
            ->group("joinhour")
            ->select();
        CsmonlineCron::_adjusthourlist($pielist, "newregisteredus", $newregisteredus);

        $daoline->where("statdate", "=", $statdate)->delete();
        foreach ($pielist as $k => $row) {
            $param = [
                "statdate" => $statdate,
                "stathour" => $k,
                "sweekday" => date("w", strtotime($statdate)),
                "ipsy" => CsmonlineCron::_gethour($row, 'ipy'),
                "pvsy" => CsmonlineCron::_gethour($row, 'pvy'),
                "uvsy" => CsmonlineCron::_gethour($row, 'uvy'),
                "vvsy" => CsmonlineCron::_gethour($row, 'vvy'),
                "pcusy" => CsmonlineCron::_gethour($row, 'pcusy'),
                "mobileusy" => CsmonlineCron::_gethour($row, 'mobileusy'),
                "ipsn" => CsmonlineCron::_gethour($row, 'ipn'),
                "pvsn" => CsmonlineCron::_gethour($row, 'pvn'),
                "uvsn" => CsmonlineCron::_gethour($row, 'uvn'),
                "vvsn" => CsmonlineCron::_gethour($row, 'vvn'),
                "pcusn" => CsmonlineCron::_gethour($row, 'pcusn'),
                "mobileusn" => CsmonlineCron::_gethour($row, 'mobileusn'),
                "allregisteredus" => CsmonlineCron::_gethour($row, 'allregisteredus'),
                "newregisteredus" => CsmonlineCron::_gethour($row, 'newregisteredus'),
                "stattime" => time()
            ];
            $daoline->create($param);
        }
    }

    private static function _gethour($pierow, $key)
    {
        if (array_key_exists($key, $pierow)) {
            return $pierow[$key]->cnt;
        } else {
            return 0;
        }
    }

    private static function _inithourpielist()
    {
        $pielist = [];
        for ($i = 0; $i < 24; $i ++) {
            $pielist[$i] = [];
        }
        return $pielist;
    }

    private static function _adjusthourlist(&$pielist, $keyname, $list)
    {
        foreach ($list as $row) {
            $pielist[$row->createhour][$keyname] = $row;
        }
    }

    // 注册会员生成 fa_csmonline_userip 表
    public static function generatefauser()
    {
        $daouser = new \app\admin\model\User();
        $daofauser = new \app\admin\model\csmonline\Fauser();
        $list = $daouser->alias('t')
            ->join('csmonline_fauser a1', ' a1.user_id=t.id', 'LEFT')
            ->where('a1.id', 'EXP', 'IS NULL')
            ->field("t.jointime,t.joinip,t.id,t.username,t.email,t.mobile")
            ->select();
        $csmip = Csmip::getInstance();
        foreach ($list as $row) {
            $region = $csmip->getRegion($row->joinip);
            $param = [
                'statdate' => date("Y-m-d", $row->jointime),
                'jointime' => $row->jointime,
                'joindate' => date("Y-m-d", $row->jointime),
                'joinhour' => date("H", $row->jointime),
                'user_id' => $row->id,
//                 'username' => $row->username,
//                 'email' => $row->email,
//                 'mobile' => $row->mobile,
                'joinip' => $row->joinip,
                'country' => $region->country,
                'region' => $region->region,
                'province' => $region->echartprovince,
                'city' => $region->city,
                'isp' => $region->isp,
                'createtime' => time()
            ];
            $daofauser->create($param);
        }
    }

    // 环境统计
    private static function _pieenv($dao, $statdate, $firstid, $envtype)
    {
        $pielist = [];
        $daopie = new \app\admin\model\csmonline\Pieevn();
        // 按区域计算游客
        if (true) {
            $ll = $dao->alias("t")
                ->where("isloginuser", "=", 'N')
                ->where("id", ">=", $firstid)
                ->where("createdate", "=", $statdate)
                ->group($envtype)
                ->field($envtype . ",count(distinct t.ip) ips,count(distinct t.userid) uvs,count(distinct t.useridmd5url) pvs,count(*) vvs")
                ->select();
            // echo $dao->getLastSql();
            foreach ($ll as $row) {
                $key = $row->$envtype == '' ? '其他' : $row->$envtype;
                if (! array_key_exists($key, $pielist)) {
                    $pielist[$key] = [];
                }
                $envvalue = $row->$envtype;
                if ($envtype == 'calismobile') {
                    $envvalue = $row->$envtype == 'Y' ? '手机' : 'PC电脑';
                }
                $pielist[$key]['statdate'] = $statdate;
                $pielist[$key]['envtype'] = $envtype;
                $pielist[$key]['envvalue'] = $envvalue;
                $pielist[$key]['ipsn'] = $row->ips;
                $pielist[$key]['uvsn'] = $row->uvs;
                $pielist[$key]['pvsn'] = $row->pvs;
                $pielist[$key]['vvsn'] = $row->vvs;
            }
        }
        // 按区域计算登录用户
        if (true) {
            $ll = $dao->alias("t")
                ->where("isloginuser", "=", 'Y')
                ->where("id", ">=", $firstid)
                ->where("createdate", "=", $statdate)
                ->group($envtype)
                ->field($envtype . ",count(distinct t.ip) ips,count(distinct t.userid) uvs,count(distinct t.useridmd5url) pvs,count(*) vvs")
                ->select();
            // echo $dao->getLastSql();
            foreach ($ll as $row) {
                $key = $row->$envtype == '' ? '其他' : $row->$envtype;
                if (! array_key_exists($key, $pielist)) {
                    $pielist[$key] = [];
                }
                $envvalue = $row->$envtype;
                if ($envtype == 'calismobile') {
                    $envvalue = $row->$envtype == 'Y' ? '手机' : 'PC电脑';
                }
                $pielist[$key]['statdate'] = $statdate;
                $pielist[$key]['envtype'] = $envtype;
                $pielist[$key]['envvalue'] = $envvalue;
                $pielist[$key]['ipsy'] = $row->ips;
                $pielist[$key]['uvsy'] = $row->uvs;
                $pielist[$key]['pvsy'] = $row->pvs;
                $pielist[$key]['vvsy'] = $row->vvs;
            }
        }
        // var_dump($pielist);
        $daopie->where("statdate", "=", $statdate)
            ->where("envtype", "=", $envtype)
            ->delete();
        foreach ($pielist as $row) {
            $daopie->create($row);
        }
    }

    private static function _adjustregionlist(&$pielist, $keyname, $list)
    {
        foreach ($list as $row) {
            $key = $row->calccountry . '-' . $row->calcprovince;
            if (! array_key_exists($key, $pielist)) {
                $pielist[$key] = [
                    "calccountry" => $row->calccountry,
                    "calcprovince" => $row->calcprovince
                ];
            }
            $pielist[$key][$keyname] = $row->$keyname;
        }
    }

    private static function _pieregion($dao, $statdate, $firstid)
    {
        $pielist = [];
        $daopie = new \app\admin\model\csmonline\Pieregion();
        // 按区域计算游客
        if (true) {
            $ll = $dao->alias("t")
                ->where("isloginuser", "=", 'N')
                ->where("id", ">=", $firstid)
                ->where("createdate", "=", $statdate)
                ->group("t.calccountry,t.calcprovince")
                ->field("t.calccountry,t.calcprovince,count(distinct t.userid) unloginus")
                ->select();
            CsmonlineCron::_adjustregionlist($pielist, 'unloginus', $ll);
        }

        // 按区域计算登录用户
        if (true) {
            $daopie = new \app\admin\model\csmonline\Pieregion();
            $ll = $dao->alias("t")
                ->where("isloginuser", "=", 'Y')
                ->where("id", ">=", $firstid)
                ->where("createdate", "=", $statdate)
                ->group("t.calccountry,t.calcprovince")
                ->field("t.calccountry,t.calcprovince,count(distinct t.userid) loginedus")
                ->select();
            CsmonlineCron::_adjustregionlist($pielist, 'loginedus', $ll);
        }

        // 按区域计算注册用户
        if (true) {
            $daofauser = new \app\admin\model\csmonline\Fauser();
            $begintime = strtotime($statdate . " 00:00:00");
            $endtime = strtotime($statdate . " 23:59:59");
            // 计算总注册人数-中国
            $ll = $daofauser->alias('t')
                ->where("jointime", "<=", $endtime)
                ->where("country", "=", '中国')
                ->group("t.country,t.province")
                ->field("t.country calccountry,t.province calcprovince,count(*) allregisteredus")
                ->select();
            CsmonlineCron::_adjustregionlist($pielist, 'allregisteredus', $ll);
            // 计算总注册人数-外国
            $ll = $daofauser->alias('t')
                ->where("jointime", "<=", $endtime)
                ->where("country", "<>", '中国')
                ->group("t.country,t.province")
                ->field("t.country calccountry,t.province calcprovince,count(*) allregisteredus")
                ->select();
            CsmonlineCron::_adjustregionlist($pielist, 'allregisteredus', $ll);

            // 计算新增注册人数-中国
            $ll = $daofauser->alias('t')
                ->where("jointime", ">=", $begintime)
                ->where("jointime", "<=", $endtime)
                ->where("country", "=", '中国')
                ->group("t.country,t.province")
                ->field("t.country calccountry,t.province calcprovince,count(*) newregisteredus")
                ->select();
            // echo $dao->getLastSql();
            CsmonlineCron::_adjustregionlist($pielist, 'newregisteredus', $ll);
            // 计算新增注册人数-外国
            $ll = $daofauser->alias('t')
                ->where("jointime", ">=", $begintime)
                ->where("jointime", "<=", $endtime)
                ->where("country", "<>", '中国')
                ->group("t.country,t.province")
                ->field("t.country calccountry,t.province calcprovince,count(*) newregisteredus")
                ->select();
            CsmonlineCron::_adjustregionlist($pielist, 'newregisteredus', $ll);
        }

        $daopie->where("statdate", "=", $statdate)->delete();
        foreach ($pielist as $row) {
            $param = [
                "statdate" => $statdate,
                "country" => $row['calccountry'],
                "province" => $row['calcprovince'],
                "unloginus" => array_key_exists('unloginus', $row) ? $row['unloginus'] : 0,
                "loginedus" => array_key_exists('loginedus', $row) ? $row['loginedus'] : 0,
                "allregisteredus" => array_key_exists('allregisteredus', $row) ? $row['allregisteredus'] : 0,
                "newregisteredus" => array_key_exists('newregisteredus', $row) ? $row['newregisteredus'] : 0,
                "createtime" => time()
            ];
            $daopie->create($param);
        }
    }


    
    public static function getguide()
    {
        $path = dirname(dirname(dirname(dirname(__FILE__))));
        $str = "
第一次安装请手动执行下任务，在菜单：在线人数和会员注册分析-手动运行定时任务，运行前7天的数据

1. 登录服务器，执行命令(root可以换成您的服务器登录名)：
crontab -e -u root

2. 在打开的文件中增加如下记录(请核对index.php目录的位置是否准确)
* 1 * * * /usr/bin/php {$path}/public/index.php /addons/csmonline/Crontab/run > /dev/null  2>&1 &

注意：（这个是VI编辑器，键盘录入a为开始进入编辑模式，修改完成后录入:wq为保存文件并退出）

3. 加载定时任务配置
service crond restart
        ";
        return $str;
    }
}

